class LinkNode{
    constructor(val){
         this.val=val;
         this.next=null
    }
    append(val){
        let cur=this;
        while(cur.next!==null){
            cur=cur.next
        }
        cur.next=val
    }
}
const testLinkNode=new LinkNode(1)
const childLinkNode=new LinkNode(2)
const childLinkNode2=new LinkNode(3)
const childLinkNode3=new LinkNode(4)
testLinkNode.append(childLinkNode)
// testLinkNode.append(childLinkNode2)
// testLinkNode.append(childLinkNode3)
function reverseList(head){
   if(!head){
       return head
   }
   let pre=null,cur=head 
   while(cur){
       let next=cur.next;
       cur.next=pre
       pre=cur
       cur=next
   }
   return pre
}
// console.log(reverseList(testLinkNode));
/**
 * 
 * @param {LinkNode} head 
 * @param {number} m  
 * @param {number} n 
 * @returns 
 */
function reverseBetween(head,m,n){
    if(m>=n){
        return head
    }
    let index=0,
    pre=null, //between 区间节点的开头
    cur, 
    leftHead,
    between,
    tail,
    dummy=new LinkNode();
    dummy.next=head
    let pointer=dummy

    while(index<m-1){
        index++
        pointer=pointer.next
    }
    leftHead=pointer //左边部分的最后节点
    between=pointer.next
    while(index<n&&pointer.next){
        index++
        pointer=pointer.next
    }   
    
    tail=pointer.next // 尾部
    pointer.next=null//  区间节点设为null
    cur=between
    pre=tail // 尾部作为区间反转部分的前驱结点
    while(cur){
        let next=cur.next;
        cur.next=pre
        pre=cur
        cur=next
    }
  
    //连接左边部分
    leftHead.next=pre
    //起始位置小于等于1代表左边部分为空,直接返回后面部分即可
    return m>1? dummy.next:pre

}

console.log(testLinkNode);
console.log(reverseBetween(testLinkNode,1,2));